--[[
* MapNotes 2.0
* File: MapNotes\MapNotes_Options.lua
* $Revision: 139 $
* $Date: 2007-01-12 23:15:39 +0000 (Fri, 12 Jan 2007) $
* 
* Copyright (c) 2006-2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]--

if (not MapNotes) then MapNotes = {}; end;
if (not MapNotes.Options) then MapNotes.Options = {}; end;
if (not MapNotes.Options.Event) then MapNotes.Options.Event = {}; end;
if (not MapNotes.Options.CategoryManagment) then MapNotes.Options.CategoryManagment = {}; end;

local L = MapNotes.Localization;

MapNotes.Options.OnLoad = function()
	-- create the frame
	local frame = CreateFrame("Frame", "MapNotesOptionsFrame");
	frame:SetWidth(727);
	frame:SetHeight(477);
	
	-- create string "MapLib"
	local fontstring = frame:CreateFontString("MapNotesOptionsHeader", "OVERLAY", "GameFontNormal");
	fontstring:SetText(L["MapNotes"]);
	fontstring:SetJustifyH("LEFT");
	fontstring:SetHeight(16);
	fontstring:SetWidth(110);
	fontstring:SetPoint("TOPLEFT", frame, "TOPLEFT", 3, -3);	

	-- not really the best way, but it is just easier to implement a new drop down for this one as I want to get this faster and don't feel like rewritng the drop down in maplib
	local fr = CreateFrame("Frame", "MapNotesOptionsCategoryManagementDropDown", frame);
	fr:SetWidth(174);
	fr:SetHeight(76);
	fr:SetPoint("CENTER", frame, "CENTER", 0, 0);
	fr:SetBackdrop({
		bgFile = "Interface\\AddOns\\MapNotes\\Artwork\\Background-PureBlack", 
		edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border", tile = true, tileSize = 32, edgeSize = 32, 
		insets = { left = 11, right = 12, top = 12, bottom = 11 }
	});
	fr:Hide();
	fr:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+3);
	
	for i = 1, 3 do
		local name = "MapNotesOptionsCategoryManagementDropDownButton"..i;
		local frame2 = CreateFrame("Button", name, MapNotesOptionsCategoryManagementDropDown);
		frame2:SetWidth(150);
		frame2:SetHeight(16);
		frame2:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+4);
		frame2:Show();
		if (i == 1) then
			frame2:SetPoint("TOPLEFT", MapNotesOptionsCategoryManagementDropDown, "TOPLEFT", 12, -14);
		else
			frame2:SetPoint("TOP", "MapNotesOptionsCategoryManagementDropDownButton"..(i - 1), "BOTTOM");
		end
		frame2:SetScript("OnClick", MapNotes.Options.CategoryManagment.DropDownOnClick);
		frame2:SetScript("OnEnter", MapNotes.Options.CategoryManagment.DropDownOnEnter);
		frame2:SetScript("OnLeave", MapNotes.Options.CategoryManagment.DropDownOnLeave);
		frame2:SetID(i);

		local texture = frame2:CreateTexture(name.."Highlight", "BACKGORUND");
		texture:SetAllPoints(true);
		texture:Hide();
		texture:SetBlendMode("ADD");
		texture:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight");
		local fontString = frame2:CreateFontString(name.."Text", "ARTWORK", "GameFontHighlightSmall");
		fontString:SetPoint("LEFT", frame2, "LEFT", 3, 0);
		fontString:SetText(L["Options_Action"][i]);
		fontString:SetWidth(150);
		fontString:SetHeight(16);
		fontString:SetJustifyH("LEFT");
	end;

	-- create the change categories menu
	-- title string
	local fontstring = frame:CreateFontString("MapNotesOptionsCategoryManagementHeader", "OVERLAY", "GameFontNormal");
	fontstring:SetText(L["Category Management"]);
	fontstring:SetJustifyH("LEFT");
	fontstring:SetPoint("TOPLEFT", frame, "TOPLEFT", 3, -40);	
	-- options box (new, edit, delete)
	local fr = CreateFrame("Frame", "MapNotesOptionsCategoryManagementActionDropDown", frame);
	fr:SetWidth(40);
	fr:SetHeight(32);
	fr:SetPoint("TOPLEFT", fontstring, "TOPLEFT", 0, -40);
	fr:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+2);
	local texture = fr:CreateTexture("MapNotesOptionsCategoryManagementActionDropDownLeft", "ARTWORK");
	texture:SetTexture("Interface\\Glues\\CharacterCreate\\CharacterCreate-LabelFrame");
	texture:SetWidth(25);
	texture:SetHeight(64);
	texture:SetPoint("TOPLEFT", fr, "TOPLEFT", 0, 17);
	texture:SetTexCoord(0, 0.1953125, 0, 1);
	texture = fr:CreateTexture("MapNotesOptionsCategoryManagementActionDropDownMiddle", "ARTWORK");
	texture:SetTexture("Interface\\Glues\\CharacterCreate\\CharacterCreate-LabelFrame");
	texture:SetWidth(115);
	texture:SetHeight(64);
	texture:SetPoint("LEFT", MapNotesOptionsCategoryManagementActionDropDownLeft, "RIGHT", 0, 0);
	texture:SetTexCoord(0.1953125, 0.8046875, 0, 1);
	texture = fr:CreateTexture("MapNotesOptionsCategoryManagementActionDropDownRight", "ARTWORK");
	texture:SetTexture("Interface\\Glues\\CharacterCreate\\CharacterCreate-LabelFrame");
	texture:SetWidth(25);
	texture:SetHeight(64);
	texture:SetPoint("LEFT", MapNotesOptionsCategoryManagementActionDropDownMiddle, "RIGHT", 0, 0);
	texture:SetTexCoord(0.8046875, 1, 0, 1);
	fontstring = fr:CreateFontString("MapNotesOptionsCategoryManagementActionDropDownText", "OVERLAY", "GameFontHighlightSmall");
--~ 	fontstring:SetText("Choose");
	fontstring:SetWidth(100);
	fontstring:SetHeight(10);
	fontstring:SetJustifyH("RIGHT");
	fontstring:SetPoint("RIGHT", texture, "RIGHT", -43, 2);	
	fontstring = fr:CreateFontString("MapNotesOptionsCategoryManagementActionDropDownHeader", "OVERLAY", "GameFontNormalSmall");
	fontstring:SetText(L["Action:"]);
	fontstring:SetJustifyH("LEFT");
	fontstring:SetPoint("TOPLEFT", fr, "TOPLEFT", 22, 11);	
	local button = CreateFrame("Button", "MapNotesOptionsCategoryManagementActionDropDownButton", fr);
	button:SetWidth(24);
	button:SetHeight(24);
	button:SetPoint("TOPRIGHT", texture, "TOPRIGHT", -16, -18);
	button:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+3);
	button:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Up");
	button:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Down");
	button:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Disabled");
	button:SetHighlightTexture("Interface\\Buttons\\UI-Common-MouseHilight");
	button:SetScript("OnClick", MapNotes.Options.CategoryManagment.ToggleActionDropDown);
	
	-- category drop down
	local fr = CreateFrame("Frame", "MapNotesOptionsCategoryManagementCategoryDropDown", frame);
	fr:SetWidth(40);
	fr:SetHeight(32);
	fr:SetPoint("LEFT", MapNotesOptionsCategoryManagementActionDropDown, "RIGHT", 100, 0);
	fr:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+2);
	local texture = fr:CreateTexture("MapNotesOptionsCategoryManagementCategoryDropDownLeft", "ARTWORK");
	texture:SetTexture("Interface\\Glues\\CharacterCreate\\CharacterCreate-LabelFrame");
	texture:SetWidth(25);
	texture:SetHeight(64);
	texture:SetPoint("TOPLEFT", fr, "TOPLEFT", 0, 17);
	texture:SetTexCoord(0, 0.1953125, 0, 1);
	texture = fr:CreateTexture("MapNotesOptionsCategoryManagementCategoryDropDownMiddle", "ARTWORK");
	texture:SetTexture("Interface\\Glues\\CharacterCreate\\CharacterCreate-LabelFrame");
	texture:SetWidth(115);
	texture:SetHeight(64);
	texture:SetPoint("LEFT", MapNotesOptionsCategoryManagementCategoryDropDownLeft, "RIGHT", 0, 0);
	texture:SetTexCoord(0.1953125, 0.8046875, 0, 1);
	texture = fr:CreateTexture("MapNotesOptionsCategoryManagementCategoryDropDownRight", "ARTWORK");
	texture:SetTexture("Interface\\Glues\\CharacterCreate\\CharacterCreate-LabelFrame");
	texture:SetWidth(25);
	texture:SetHeight(64);
	texture:SetPoint("LEFT", MapNotesOptionsCategoryManagementCategoryDropDownMiddle, "RIGHT", 0, 0);
	texture:SetTexCoord(0.8046875, 1, 0, 1);
	fontstring = fr:CreateFontString("MapNotesOptionsCategoryManagementCategoryDropDownText", "OVERLAY", "GameFontHighlightSmall");
	fontstring:SetWidth(100);
	fontstring:SetHeight(10);
	fontstring:SetJustifyH("RIGHT");
--~ 	fontstring:SetText("Select Category");
	fontstring:SetPoint("RIGHT", texture, "RIGHT", -43, 2);	
	fontstring = fr:CreateFontString("MapNotesOptionsCategoryManagementCategoryDropDownText", "OVERLAY", "GameFontNormalSmall");
	fontstring:SetText(L["Category:"]);
	fontstring:SetJustifyH("LEFT");
	fontstring:SetPoint("TOPLEFT", fr, "TOPLEFT", 22, 11);	
	local button = CreateFrame("Button", "MapNotesOptionsCategoryManagementCategoryDropDownButton", fr);
	button:SetWidth(24);
	button:SetHeight(24);
	button:SetPoint("TOPRIGHT", texture, "TOPRIGHT", -16, -18);
	button:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+3);
	button:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Up");
	button:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Down");
	button:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Disabled");
	button:SetHighlightTexture("Interface\\Buttons\\UI-Common-MouseHilight");
	button:SetScript("OnClick", MapNotes.Options.CategoryManagment.ToggleCategoryDropDown);

		-- icon drop down
	local fr = CreateFrame("Frame", "MapNotesOptionsCategoryManagementIconDropDown", frame);
	fr:SetWidth(40);
	fr:SetHeight(32);
	fr:SetPoint("LEFT", MapNotesOptionsCategoryManagementCategoryDropDown, "RIGHT", 120, 0);
	fr:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+2);
	local texture = fr:CreateTexture("MapNotesOptionsCategoryManagementIconDropDownLeft", "ARTWORK");
	texture:SetTexture("Interface\\AddOns\\MapNotes\\Artwork\\IconDropDown-LabelFrame");
	texture:SetWidth(48);
	texture:SetHeight(64);
	texture:SetPoint("TOPLEFT", fr, "TOPLEFT", 0, 17);
	texture:SetTexCoord(0, 0.375, 0, 1);
	texture = fr:CreateTexture("MapNotesOptionsCategoryManagementIconDropDownMiddle", "ARTWORK");
	texture:SetTexture("Interface\\AddOns\\MapNotes\\Artwork\\IconDropDown-LabelFrame");
	texture:SetWidth(80);
	texture:SetHeight(64);
	texture:SetPoint("LEFT", MapNotesOptionsCategoryManagementIconDropDownLeft, "RIGHT", 0, 0);
	texture:SetTexCoord(0.375, 0.8046875, 0, 1);
	texture = fr:CreateTexture("MapNotesOptionsCategoryManagementIconDropDownRight", "ARTWORK");
	texture:SetTexture("Interface\\AddOns\\MapNotes\\Artwork\\IconDropDown-LabelFrame");
	texture:SetWidth(25);
	texture:SetHeight(64);
	texture:SetPoint("LEFT", MapNotesOptionsCategoryManagementIconDropDownMiddle, "RIGHT", 0, 0);
	texture:SetTexCoord(0.8046875, 1, 0, 1);
	fontstring = fr:CreateFontString("MapNotesOptionsCategoryManagementIconDropDownText", "OVERLAY", "GameFontHighlightSmall");
	fontstring:SetWidth(100);
	fontstring:SetHeight(10);
	fontstring:SetJustifyH("RIGHT");
	fontstring:SetText(L["Select Icon"]);
	fontstring:SetPoint("RIGHT", texture, "RIGHT", -43, 2);	
--~ 	fontstring = fr:CreateFontString("MapNotesOptionsCategoryManagementCategoryDropDownText", "OVERLAY", "GameFontNormalSmall");
--~ 	fontstring:SetText("Category:");
--~ 	fontstring:SetJustifyH("LEFT");
--~ 	fontstring:SetPoint("TOPLEFT", fr, "TOPLEFT", 22, 14);	
	local button = CreateFrame("Button", "MapNotesOptionsCategoryManagementIconDropDownButton", fr);
	button:SetWidth(24);
	button:SetHeight(24);
	button:SetPoint("TOPRIGHT", texture, "TOPRIGHT", -16, -18);
	button:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+3);
	button:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Up");
	button:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Down");
	button:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Disabled");
	button:SetHighlightTexture("Interface\\Buttons\\UI-Common-MouseHilight");
	texture = fr:CreateTexture("MapNotesOptionsCategoryManagementIconDropDownTexture", "OVERLAY");
	texture:SetWidth(16);
	texture:SetHeight(16);
	texture:SetPoint("CENTER", "MapNotesOptionsCategoryManagementIconDropDownLeft", "LEFT", 23, 1);
	texture:SetTexture("Interface\\AddOns\\MapNotes\\Icons\\NOT_FOUND");
	button:SetScript("OnClick", MapNotes.Options.CategoryManagment.ToggleIconDropDown);
	
	-- edit box
	local editbox = CreateFrame("EditBox", "MapNotesOptionsCategoryManagementEditBox", frame);
	--editbox:SetHistoryLines("0");
	editbox:SetMaxLetters(15);
	editbox:SetAutoFocus(false);
	editbox:SetWidth(230);
	editbox:SetHeight(64);
	editbox:SetPoint("LEFT", fr, "RIGHT", 110, 4); -- SetPoint("TOPLEFT", frame, "TOPLEFT", 32, -95);
	editbox:SetFontObject("GameFontHighlightSmall");
	editbox:EnableKeyboard(true);
	editbox:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+3);
	editbox:SetScript("OnEscapePressed", function(this) this:GetParent():Hide(); end);
	editbox:SetScript("OnTextChanged", MapNotes.Options.CategoryManagment.CheckIfSaveButtonShouldBeEnabled);
--~ 	editbox:SetScript("OnTextChanged", function(this) if (this:GetText() == "" or this:GetText() == nil) then MapNotesShareSendButton:Disable(); else MapNotesShareSendButton:Enable(); end; end);
	local background = CreateFrame("Frame", "MapNotesOptionsCategoryManagementEditBoxBackground", editbox);
	background:SetWidth(240); background:SetHeight(23);
	background:SetPoint("TOPLEFT", editbox, "TOPLEFT", -5, -22);
	background:SetBackdrop({ 
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", 
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tile = true, tileSize = 16, edgeSize = 16, 
		insets = { left = 5, right = 5, top = 5, bottom = 5 }
	});
	background:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+3);
	background:SetBackdropBorderColor(TOOLTIP_DEFAULT_COLOR.r, TOOLTIP_DEFAULT_COLOR.g, TOOLTIP_DEFAULT_COLOR.b);
	background:SetBackdropColor(TOOLTIP_DEFAULT_BACKGROUND_COLOR.r, TOOLTIP_DEFAULT_BACKGROUND_COLOR.g, TOOLTIP_DEFAULT_BACKGROUND_COLOR.b);
	font = background:CreateFontString(nil, "ARTWORK", "GameFontNormalSmall");
	font:SetPoint("TOPLEFT", background, "TOPLEFT", 4, 10);
	font:SetText(L["New category name:"]);

	-- create button
	button = CreateFrame("Button", "MapNotesOptionsCategoryManagementSaveButton", frame, "UIPanelButtonTemplate");
--~ 	button:SetText("Do Changes");
	button:SetWidth(150);
	button:SetHeight(21);
	button:SetFrameLevel(MAPNOTES_FRAMELEVEL_EDITMENU+3);
	button:SetPoint("TOPLEFT", MapNotesOptionsCategoryManagementHeader, "TOPLEFT", 535, -70);
	button:SetScript("OnClick", MapNotes.Options.CategoryManagment.OnSaveClicked);
	
	-- a help fontstring
	font = frame:CreateFontString("MapNotesOptionsCategoryManagementHelp", "BACKGROUND", "GameFontNormalSmall");
	font:SetPoint("TOPLEFT", MapNotesOptionsCategoryManagementHeader, "TOPLEFT", 18, -75);
--~ 	font:SetText("Note: Category Management changes can not be undone by hitting the Cancel or Reset button.");
	
	MapLib.Options.RegisterMenu(L["MapNotes"], frame);
	MapLib.EventHandler.RegisterEvent("MAPNOTES_OPTIONS", "MAPLIB_OPTIONS_RESET", MapNotes.Options.MAPLIB_OPTIONS_RESET);
	MapLib.EventHandler.RegisterEvent("MAPNOTES_OPTIONS", "MAPLIB_OPTIONS_DEFAULT", MapNotes.Options.MAPLIB_OPTIONS_DEFAULT);
	MapLib.EventHandler.RegisterEvent("MAPNOTES_OPTIONS", "MAPLIB_OPTIONS_SAVE", MapNotes.Options.MAPLIB_OPTIONS_SAVE);
	MapNotes.Options.OnLoad = nil;
end;

MapNotes.Options.MAPLIB_OPTIONS_RESET = function()
	MapNotes.Options.ClearFocus_CloseDropDown();
	MapNotes.Options.CategoryManagment.Reset();
end;

MapNotes.Options.MAPLIB_OPTIONS_DEFAULT = function()
	MapNotes.Options.ClearFocus_CloseDropDown();
	MapNotes.Options.CategoryManagment.Reset();
end;

MapNotes.Options.MAPLIB_OPTIONS_SAVE = function()
	
end;

MapNotes.Options.CategoryManagment.Reset = function()
	MapNotesOptionsCategoryManagementActionDropDownText:SetText("Choose");
	MapNotesOptionsCategoryManagementCategoryDropDown:Hide();
	MapNotesOptionsCategoryManagementIconDropDown:Hide();
	MapNotesOptionsCategoryManagementEditBox:Hide();
	MapNotesOptionsCategoryManagementSaveButton:Hide();
	MapNotesOptionsCategoryManagementHelp:SetText(L["Note: Category Management changes can not be undone by hitting the Cancel or Reset button."]);
end;

MapNotes.Options.CategoryManagment.ToggleCategoryDropDown = function()
	if ( not MapNotes.Category.DropDown.IsVisible() ) then
		MapNotes.Options.ClearFocus_CloseDropDown("categorydropdown");
		MapNotes.Category.DropDown.Show(noHighlight, 0, MapNotes.Options.CategoryManagment.SetCategory, MapNotesOptionsFrame, MAPNOTES_FRAMELEVEL_EDITMENU+4,"TOPLEFT", MapNotesOptionsCategoryManagementCategoryDropDown, "TOPLEFT", 12, -22);
	else
		MapNotes.Category.DropDown.Hide();
	end;
end;

MapNotes.Options.CategoryManagment.CheckIfSaveButtonShouldBeEnabled = function()
	if (MapNotes.Options.currentMode == "NEW") then
		local editbox = MapNotesOptionsCategoryManagementEditBox:GetText();
		-- there needs to be text in the editbox and that must be different of all known categories
		local index;
		for k, v in ipairs(MapNotesSaved_Category.categories) do
			if (v.name == editbox) then
				index = k;
				break;
			end;
		end;
		if ( editbox and editbox ~= "" and not index and MapNotes.Options.CategoryManagment.currentIcon) then
			MapNotesOptionsCategoryManagementSaveButton:Enable();
			MapNotesOptionsCategoryManagementHelp:SetText(L["Note: Hit 'Create Category' to create the new category."]);
		else
			MapNotesOptionsCategoryManagementSaveButton:Disable();
			MapNotesOptionsCategoryManagementHelp:SetText(L["Note: Select an icon and choose a name for the new category."]);
		end;
	elseif (MapNotes.Options.currentMode == "EDIT") then
		local editbox = MapNotesOptionsCategoryManagementEditBox:GetText();
		-- there needs to be text in the editbox and that must be different of all known categories
		local index;
		for k, v in ipairs(MapNotesSaved_Category.categories) do
			if (v.name == editbox) then
				index = k;
				break;
			end;
		end;
		-- if there is no category named that way enable,
		-- is thecategory is equal but icon changed = enable,
		if ( editbox and editbox ~= "" and (not index or (MapNotes.Options.CategoryManagment.currentIcon ~= MapNotesSaved_Category.categories[index].defaultIcon and editbox == MapNotes.Options.CategoryManagment.currentCategory)) ) then
			MapNotesOptionsCategoryManagementSaveButton:Enable();
			MapNotesOptionsCategoryManagementHelp:SetText(string.format(L["Note: Hit 'Edit Category' to confirm the changes to the category '%s'."], MapNotes.Options.CategoryManagment.currentCategory));
		else
			MapNotesOptionsCategoryManagementSaveButton:Disable();
			MapNotesOptionsCategoryManagementHelp:SetText(L["Note: Select an new icon or a new name for the chosen category."]);
		end;
	elseif (MapNotes.Options.currentMode == "DELETE") then
		if (MapNotes.Options.CategoryManagment.currentCategory) then
			MapNotesOptionsCategoryManagementSaveButton:Enable();
			MapNotesOptionsCategoryManagementHelp:SetText(string.format(L["Note: Hit 'Delete Category' to confirm deletion of '%s'."], MapNotes.Options.CategoryManagment.currentCategory));
		else
			MapNotesOptionsCategoryManagementSaveButton:Disable();
			MapNotesOptionsCategoryManagementHelp:SetText(L["Note: Select a category."]);
		end;
	end;
end;

MapNotes.Options.CategoryManagment.OnSaveClicked = function()
	local oldName = MapNotes.Options.CategoryManagment.currentCategory;
	local newName = MapNotesOptionsCategoryManagementEditBox:GetText();
	local newIcon = MapNotes.Options.CategoryManagment.currentIcon;
	if (MapNotes.Options.currentMode == "NEW") then
		-- create the new category
		tinsert(MapNotesSaved_Category.categories, {name = newName, defaultIcon = newIcon});
	elseif (MapNotes.Options.currentMode == "EDIT") then
	-- edit the exsi
		for k, v in ipairs(MapNotesSaved_Category.categories) do
			if (v.name == oldName) then
				v.name = newName; v.defaultIcon = newIcon;
				-- update all notes:
				for _, note in pairs(MapNotesSaved_NoteDB) do
					if (type(note) == "table" and note.category == oldName) then
						note.category = newName;
					end;
				end;
			end;
		end;
	elseif (MapNotes.Options.currentMode == "DELETE") then
		-- delete
		for k, v in ipairs(MapNotesSaved_Category.categories) do
			if (v.name == oldName) then
				tremove(MapNotesSaved_Category.categories, k);
				-- update all notes in that category to general:
				for _, note in pairs(MapNotesSaved_NoteDB) do
					if (type(note) == "table" and note.category == oldName) then
						note.category = MapNotes.Category.generalCategory.name;
					end;
				end;
			end;
		end;
	end;
	-- Repush the submenu to maplib
	MapLib.POI.UpdateSubmenu("MAPNOTES", MapNotes.BuiltSubmenuTable());
	MapNotes.Options.CategoryManagment.Reset();
end;

MapNotes.Options.CategoryManagment.DropDownOnEnter = function()
	getglobal(this:GetName().."Highlight"):Show();
end;

MapNotes.Options.CategoryManagment.DropDownOnLeave = function()
	getglobal(this:GetName().."Highlight"):Hide();
end;

MapNotes.Options.CategoryManagment.DropDownOnClick = function()
	local action = this:GetID();
	MapNotesOptionsCategoryManagementActionDropDownText:SetText(L["Options_Action"][action]);
	MapNotes.Options.ClearFocus_CloseDropDown();
	if (action == 1) then
		-- action = 1, new
		-- hide the category drop down
		MapNotesOptionsCategoryManagementCategoryDropDown:Hide();
		-- display icon without an icon chosen
		MapNotesOptionsCategoryManagementIconDropDown:Show();
		MapNotes.Options.CategoryManagment.currentIcon = nil;
		MapNotes.Options.CategoryManagment.UpdateIcon();
		-- display the editbox
		MapNotesOptionsCategoryManagementEditBox:Show();
		MapNotesOptionsCategoryManagementEditBox:SetText("");
		-- show save button and disable
		MapNotesOptionsCategoryManagementSaveButton:Show();
		MapNotesOptionsCategoryManagementSaveButton:Disable();
		MapNotesOptionsCategoryManagementSaveButton:SetText(L["Create Category"]);
		
		MapNotesOptionsCategoryManagementHelp:SetText(L["Note: Select an icon and choose a name for the new category."]);
		-- set to "new"
		MapNotes.Options.currentMode = "NEW";
	elseif (action == 2) then
		-- action = 2, edit
		-- display category drop down without a category chosen
		MapNotesOptionsCategoryManagementCategoryDropDown:Show();
		MapNotesOptionsCategoryManagementCategoryDropDownText:SetText(L["Select Category"]);
		MapNotes.Options.CategoryManagment.currentCategory = nil;
		-- NOT display icon untill a category is chosen
		MapNotesOptionsCategoryManagementIconDropDown:Hide();
		-- NOT display edit untill a category is chosen
		MapNotesOptionsCategoryManagementEditBox:Hide();
		MapNotesOptionsCategoryManagementSaveButton:Show();
		MapNotesOptionsCategoryManagementSaveButton:Disable();
		MapNotesOptionsCategoryManagementSaveButton:SetText(L["Edit Category"]);
		
		MapNotesOptionsCategoryManagementHelp:SetText(L["Note: Select a category."]);
		MapNotes.Options.currentMode = "EDIT";
	elseif (action == 3) then
		-- action = 3, delete
		-- display category
		MapNotesOptionsCategoryManagementCategoryDropDown:Show();
		MapNotesOptionsCategoryManagementCategoryDropDownText:SetText(L["Select Category"]);
		MapNotes.Options.CategoryManagment.currentCategory = nil;
		-- hide icon + editbox
		MapNotesOptionsCategoryManagementIconDropDown:Hide();
		MapNotesOptionsCategoryManagementEditBox:Hide();
		MapNotesOptionsCategoryManagementSaveButton:Show();
		MapNotesOptionsCategoryManagementSaveButton:Disable();
		MapNotesOptionsCategoryManagementSaveButton:SetText(L["Delete Category"]);
		
		MapNotesOptionsCategoryManagementHelp:SetText(L["Note: Select a category."]);
		MapNotes.Options.currentMode = "DELETE";
	end;
	-- for all show the save button with correct text.
	-- show the correct help text
end;

MapNotes.Options.CategoryManagment.ToggleActionDropDown = function()
	if ( not MapNotesOptionsCategoryManagementDropDown:IsVisible() ) then
		MapNotes.Options.ClearFocus_CloseDropDown("actiondropdown");
		MapNotesOptionsCategoryManagementDropDown:Show();
		MapNotesOptionsCategoryManagementDropDown:SetPoint("TOPLEFT", MapNotesOptionsCategoryManagementActionDropDown, "TOPLEFT", 12, -22);
	else
		MapNotesOptionsCategoryManagementDropDown:Hide();
	end;
end;

MapNotes.Options.ClearFocus_CloseDropDown = function(exception)
	if (exception ~= "actiondropdown") then MapNotesOptionsCategoryManagementDropDown:Hide(); end;
	if (exception ~= "categorydropdown") then MapNotes.Category.DropDown.Hide(); end;
	if (exception ~= "icondropdown") then MapNotes.Icon.DropDown.Hide(); end;
	if (exception ~= "newcategoryeditbox") then MapNotesOptionsCategoryManagementEditBox:ClearFocus(); end;
end;

MapNotes.Options.CategoryManagment.SetCategory = function(name)
--	MapNotes.EditMenu.categoryName = name;
--	MapNotes.EditMenu.CategoryDropDown.Update();
--	MapNotes.EditMenu.IconDropDown.Update();
-- set the name
	MapNotesOptionsCategoryManagementCategoryDropDownText:SetText(name);
	MapNotes.Options.CategoryManagment.currentCategory = name;
	if (MapNotes.Options.currentMode == "EDIT") then
		MapNotesOptionsCategoryManagementIconDropDown:Show();
		-- set icon to category default
		-- show icon texture
		-- find the category number:
		local index;
		for k, v in ipairs(MapNotesSaved_Category.categories) do
			if (v.name == name) then
				index = k;
				break;
			end;
		end;
		MapNotes.Options.CategoryManagment.currentIcon = MapNotesSaved_Category.categories[index].defaultIcon;
		MapNotes.Options.CategoryManagment.UpdateIcon();
		-- set editbox to name
		MapNotesOptionsCategoryManagementEditBox:SetText(name);
		MapNotesOptionsCategoryManagementEditBox:Show();
	end;
	MapNotes.Options.ClearFocus_CloseDropDown();
	MapNotes.Options.CategoryManagment.CheckIfSaveButtonShouldBeEnabled();
end;

MapNotes.Options.CategoryManagment.ToggleIconDropDown = function()
	if ( not MapNotes.Icon.DropDown.IsVisible() ) then
		MapNotes.Options.ClearFocus_CloseDropDown("icondropdown");
		MapNotes.Icon.DropDown.Show(noHighlight, 0, MapNotes.Options.CategoryManagment.SetIcon, MapNotesOptionsFrame, MAPNOTES_FRAMELEVEL_EDITMENU+4,"TOPLEFT", MapNotesOptionsCategoryManagementIconDropDown, "TOPLEFT", 0, -22);
	else
		MapNotes.Icon.DropDown.Hide();
	end;
end;

MapNotes.Options.CategoryManagment.SetIcon = function(iconID)
--	MapNotes.EditMenu.iconID = iconID;
--	MapNotes.EditMenu.IconDropDown.Update();
	-- set icon
	MapNotes.Options.CategoryManagment.currentIcon = iconID;
	MapNotes.Options.CategoryManagment.UpdateIcon();
	-- show icon texture
	MapNotes.Options.ClearFocus_CloseDropDown();
	MapNotes.Options.CategoryManagment.CheckIfSaveButtonShouldBeEnabled();
end;

MapNotes.Options.CategoryManagment.UpdateIcon = function()
	local icon = MapNotes.Options.CategoryManagment.currentIcon;
	local texture = MapNotesOptionsCategoryManagementIconDropDownTexture;
	if (not icon) then texture:Hide(); return; end;
	icon = MapNotes.Icon.GetIconTexture(icon);
	texture:SetTexture(icon.file);
	if (icon.size < 24) then
		texture:SetWidth(icon.size); texture:SetHeight(icon.size);
	else
		texture:SetWidth(24); texture:SetHeight(24);
	end;
	if (icon.tcoordMinX) then texture:SetTexCoord(icon.tcoordMinX, icon.tcoordMaxX, icon.tcoordMinY, icon.tcoordMaxY); else texture:SetTexCoord(0, 1, 0, 1); end;
	if (icon.colorR) then texture:SetVertexColor(icon.colorR, icon.colorG, icon.colorB, icon.colorA); else texture:SetVertexColor(1, 1, 1, 1); end;
	texture:Show();
end;


MapNotes.Options.OnLoad();